package com.happy.entity;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.core.utils.ReflectionUtils;


/**
 * 受保护的资源.
 * 
 * 注释见{@link User}.
 */
@Entity
@Table(name = "resource", catalog = "sec", uniqueConstraints = {})
public class Resource implements java.io.Serializable {
	// resourceType常量 //
	public static final String URL_TYPE = "url";
	public static final String MENU_TYPE = "menu";

	// Fields

	private Long nuId;
	private String vcResourceType;
	private String vcValue;
	private double nuPosition;
	
	private List<Authority> authorityList = new ArrayList<Authority>();
	
	// Constructors

	/** default constructor */
	public Resource() {
	}

	/** full constructor */
	public Resource(Long nuId, double nuPosition, String vcResourceType,
			String vcValue) {
		this.nuId = nuId;
		this.nuPosition = nuPosition;
		this.vcResourceType = vcResourceType;
		this.vcValue = vcValue;
	}

	// Property accessors
//	@Id
//	@GeneratedValue(generator = "resource")
//	@GenericGenerator(name = "resource", strategy = "sequence", parameters = { @Parameter(name = "sequence", value = "TMS_OWN.SEQ_TMS_RESOURCE") })
//	@Column(name = "NU_ID", unique = false, nullable = false, insertable = true, updatable = true, precision = 10, scale = 0)
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false, insertable = true, updatable = true)
	public Long getNuId() {
		return this.nuId;
	}

	public void setNuId(Long nuId) {
		this.nuId = nuId;
	}

	/**
	 * 资源类型.
	 */
	@Column(name = "VC_RESOURCE_TYPE", unique = false, nullable = false, insertable = true, updatable = true, length = 20)
	public String getVcResourceType() {
		return this.vcResourceType;
	}

	public void setVcResourceType(String vcResourceType) {
		this.vcResourceType = vcResourceType;
	}
	
	/**
	 * 资源标识.
	 */
	@Column(name = "VC_VALUE", unique = false, nullable = false, insertable = true, updatable = true)
	public String getVcValue() {
		return this.vcValue;
	}

	public void setVcValue(String vcValue) {
		this.vcValue = vcValue;
	}
	
	/**
	 * 资源在SpringSecurity中的校验顺序字段.
	 */
	@Column(name = "NU_POSITION", unique = false, nullable = false, insertable = true, updatable = true, precision = 10, scale = 0)
	public double getNuPosition() {
		return this.nuPosition;
	}

	public void setNuPosition(double nuPosition) {
		this.nuPosition = nuPosition;
	}
	
	/**
	 * 可访问该资源的授权集合.
	 */
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
	@JoinTable(schema="sec", name = "resource_authority", joinColumns = { @JoinColumn(name = "NU_RESC_ID") }, inverseJoinColumns = { @JoinColumn(name = "NU_AUTH_ID") })
	@Fetch(FetchMode.SUBSELECT)
	public List<Authority> getAuthorityList() {
		return authorityList;
	}

	public void setAuthorityList(List<Authority> authorityList) {
		this.authorityList = authorityList;
	}

	/**
	 * 可访问该资源的授权名称字符串, 多个授权用','分隔.
	 * @throws Exception 
	 */
	@Transient
	public String getAuthNames() throws Exception {
		return ReflectionUtils.convertElementPropertyToString(authorityList, "authorityName", ",");
	}
	@Transient
	public List<Long> getList() throws Exception
	{
         return ReflectionUtils.convertElementPropertyToList(authorityList, "id");
	}
}
